<template>
  <el-dialog :title="title" :visible="visible" @close="handleClose" width="800px">
    <el-table v-if="frozen.length>0" style="margin-top: 10px;" :data="frozen" height="405px">
      <el-table-column prop="MsfxWareHouseName" label="单据名称"></el-table-column>
      <el-table-column prop="Remark" label="单据类型"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button icon="el-icon-switch-button" type="text" @click="closeTable(scope.row)">解冻</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-if="codeList.length>0" style="margin-top: 10px;" :data="codeList" height="405px" @selection-change="handleSelectionChange">
      <el-table-column prop="skuBarId" label="追溯码"></el-table-column>
      <el-table-column prop="skuParentId" label="父级Id"></el-table-column>
<!--      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button icon="el-icon-switch-button" type="text" @click="sukIdClose(scope.row)">解冻</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 单据类型数量冻结 -->
    <el-table v-if="typeNum.length>0" style="margin-top: 10px;" :data="typeNum" height="405px">
      <el-table-column prop="name" label="冻结类型"></el-table-column>
      <el-table-column prop="num" label="冻结数量"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button icon="el-icon-switch-button" type="text" @click="closeTableType(scope.row)">解冻</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="frozen.length==0&&codeList.length==0&&typeNum.length==0" style="display: flex;justify-content: center;align-items: center;margin-bottom: 20px;">
      暂无数据
    </div>
    <el-pagination v-if="!frozen.length==0&&!codeList.length==0&&!typeNum.length==0" background layout=" total,prev, pager, next, sizes,jumper" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="pageNum" :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]" :total="total">
    </el-pagination>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    findLimitationInfo ,//查看明细
    putBillsTypeEnableStatus,//状态解冻
    putBatchEnableStatus//追溯码解冻
  } from '@/api/storeM/index.js'
  export default {
    data() {
      return {
        frozen: [],//单据类型
        codeList:[],//追溯码
        title: '查看&解冻',
        dataForm: {},
        pageNum: 1,
        pageSize: 10,
        total: 0,
        commonList:[],
        selectList:[],//选中的追溯
        typeNum:[],//类型数量冻结
      }
    },
    props: {
      visible: { // 接收父组件通过sync传递的visible属性
        type: Boolean,
        default: false
      }
    },
    methods: {
      //调用接口拿到table数据
      getFrozenList() {
        findLimitationInfo(this.dataForm).then(res => {
          // 判断 '单据类型冻结' 是否为非空数组
          if (Array.isArray(res.data['单据类型冻结']) && res.data['单据类型冻结'].length > 0) {
            this.frozen = res.data['单据类型冻结'];
            this.total = this.frozen.length;
          }else{
            this.frozen = [];
          }
          //判断'单据类型数量冻结'
          if(Array.isArray(res.data['数量冻结']) && res.data['数量冻结'].length > 0){
            this.typeNum = res.data['数量冻结'];
            this.total = this.typeNum.length;
          }else{
            this.typeNum = [];
          }
          // 判断 '追溯码限制' 是否为非空数组
          if (res.data['追溯码限制']!=null) {
            this.codeList = res.data['追溯码限制'].skuList?res.data['追溯码限制'].skuList:[];
            this.commonList = res.data['追溯码限制'].limitation;
            this.total = this.codeList.length;
          }else{
            this.codeList = []
          }
        }).catch(err => {
          console.error('请求失败:', err);
        });
      },
      handleSelectionChange(val){
        this.selectList = val.map(item => item.id);
      },
      //解冻追溯码
      sukIdClose(){
        let param = {
          pkMaterial:this.commonList[0].pk_material,
          pkBatchcode:this.commonList[0].pk_batchcode,
          id:this.commonList[0].id
        }
        let data = this.selectList;
        putBatchEnableStatus(param,data).then(res=>{
          this.$message(res.data);
          this.getFrozenList();
        })
      },
      //解冻数量
      closeTableType(data) {
        let param = {
          id: data.id,
          name:this.dataForm.name,
          vbatchcode:this.dataForm.vbatchcode
        }
        putBillsTypeEnableStatus(param).then(res => {
          this.$message.success(res.msg);
          this.getFrozenList();
          this.$emit('successfun');
        })
      },
      //分页
      handleSizeChange(newsize) {
        this.pageNum = 1
        this.pageSize = newsize
        this.getFrozenList()
      },
      handleCurrentChange(newpage) {
        this.pageNum = newpage
        this.getFrozenList()
      },
      //打开table
      openDialog(row) {
        this.dataForm.pkMaterial = row.pk_material;
        this.dataForm.pkBatchCode = row.pk_batchcode;
        this.dataForm.name = row.name;
        this.dataForm.vbatchcode = row.vbatchcode;
        this.getFrozenList();
      },
      //关闭关联
      closeTable(data) {
        let param = {
          id: data.id,
          name:this.dataForm.name,
          vbatchcode:this.dataForm.vbatchcode
        }
        putBillsTypeEnableStatus(param).then(res => {
          this.$message.success(res.msg);
          this.getFrozenList();
        })
      },
      // 提取重置表单的逻辑
      handleClose() {
        this.$emit('update:visible', false);
        this.selectList = [];
      },
    }
  }
</script>

<style>
</style>
